package com.hxzy.driver.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.hxzy.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author wy
 * @Description nacos服务发现控制器
 * @Date 2023/12/12 14:31
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/service")
public class ServiceDiscoveryController {

    /**
     * springcloud的服务实例对象
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 根据服务名，获取nacos中可用的服务列表
     * @param serviceName
     * @return
     */
     @GetMapping(value = "/byname/{serviceName}")
    public R  findServiceName(@PathVariable(value = "serviceName") String serviceName) throws NacosException {
         List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceName);
         return R.ok(instances);
     }



}
